//
//  DetailsController_img.m

//
//  Created by dllo on 15/9/25.
//  Copyright (c) 2015年 Lx. All rights reserved.
//

#import "ZGLImgVC.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "ZGLHTTPTOOL.h"
@interface ZGLImgVC ()<UIScrollViewDelegate, UIWebViewDelegate>
@property(nonatomic, retain)NSArray *dicArr;
@property(nonatomic, retain)NSDictionary *modDic;
@property(nonatomic, retain)UIScrollView *scroll;
@property(nonatomic, retain)UILabel *theTitle;
@property(nonatomic, retain)UILabel *page;
@property(nonatomic, retain)UIWebView *web;
@end

@implementation ZGLImgVC

//隐藏tabbar
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"clear"] forBarMetrics:UIBarMetricsDefault];
//    [self.navigationController.navigationBar.layer setMasksToBounds:YES];//剪掉多余阴影
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = NO;
    self.automaticallyAdjustsScrollViewInsets = YES;
//    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"zaihufanhui"] style:(UIBarButtonItemStyleDone) target:self action:@selector(leftBarButtonItem:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    //铺界面
    _scroll = [[UIScrollView alloc]init];
    _scroll.backgroundColor = [UIColor blackColor];
    _scroll.pagingEnabled = YES;
    _scroll.showsHorizontalScrollIndicator = NO;
    _scroll.showsVerticalScrollIndicator= NO;
    _scroll.delegate = self;
    _scroll.bounces = NO;
    _scroll.frame = CGRectMake(0, 0, Width, Height);
    [self.view addSubview:_scroll];
    
    _theTitle = [[UILabel alloc]initWithFrame:CGRectMake(20, 490*ScaleX, 270*ScaleX, 20)];
    _theTitle.backgroundColor = [UIColor clearColor];
    _theTitle.textColor = [UIColor whiteColor];
    _theTitle.font = [UIFont systemFontOfSize:17];
    [self.view addSubview:_theTitle];
    
    _page = [[UILabel alloc]initWithFrame:CGRectMake(270*ScaleX+30, 490*ScaleX, 65, 20)];
    _page.backgroundColor = [UIColor clearColor];
    _page.textAlignment = NSTextAlignmentRight;
    _page.textColor = [UIColor whiteColor];
    _page.font = [UIFont systemFontOfSize:17];
    [self.view addSubview:_page];
    
    _web = [[UIWebView alloc]init];
    _web.frame = CGRectMake(10, 490*ScaleX+30, 355*ScaleX, 140*ScaleX);
    _web.opaque = NO; //不设置这个值 页面背景始终是白色
    _web.backgroundColor = [UIColor blackColor];
    _web.delegate = self;
//    [(UIScrollView *)[[_web subviews] objectAtIndex:0] setBounces:NO];//禁止反弹
    [self.view addSubview:_web];
    [self handData];
}
- (void)leftBarButtonItem:(UIBarButtonItem *)button {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)handData{
    __weak typeof(self) weakSelf = self;
    [ZGLHTTPTOOL GETWithURL:[NSString stringWithFormat:@"http://c.3g.163.com/photo/api/set/%@.json", self.skipID] withBody:nil withHttpHead:nil responseStyle:JSON withSuccess:^(id result) {
        weakSelf.modDic = [[NSDictionary alloc]initWithDictionary:result];
        weakSelf.dicArr = [_modDic objectForKey:@"photos"];
        [weakSelf setData];
    }withFail:^(NSError *error) {

    }];
}

//赋值
- (void)setData{
    _scroll.contentSize = CGSizeMake(Width*_dicArr.count, 1);
    for (int i=0; i<_dicArr.count; i++){
        UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(i*Width, 0, Width, Height)];
        NSDictionary *dic = [[NSDictionary alloc]initWithDictionary:_dicArr[i]];
        img.contentMode = UIViewContentModeScaleAspectFit;//高度自适应
        [img sd_setImageWithURL:[dic objectForKey:@"imgurl"] placeholderImage:[UIImage imageNamed:@"squirrel"]];
        [self.scroll addSubview:img];
    }
    
    _theTitle.text = [_modDic objectForKey:@"setname"];
    _page.text = [NSString stringWithFormat:@"1/%lu", _dicArr.count];
    NSDictionary *dic = [[NSDictionary alloc]initWithDictionary:_dicArr[0]];
    [_web loadHTMLString: [dic objectForKey:@"note"] baseURL:nil];
}

//必须在这里设置webview的背景和字体颜色
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    webView.backgroundColor = [UIColor clearColor];
    [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'white'"];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    int a = scrollView.contentOffset.x/Width;
    _page.text = [NSString stringWithFormat:@"%d/%lu", a+1, _dicArr.count];
    NSDictionary *dic = [[NSDictionary alloc]initWithDictionary:_dicArr[a]];
    [_web loadHTMLString: [dic objectForKey:@"note"] baseURL:nil];
}

- (void)dealloc{
    _scroll.delegate = nil;
    _web.delegate = nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
